package com.jf.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jf.blog.bean.*;
import com.jf.blog.form.comment.CommentForm;
import com.jf.blog.mapper.BlogMapper;
import com.jf.blog.mapper.TimelineMapper;
import com.jf.blog.service.*;
import com.jf.blog.util.LoginUtil;
import com.jf.blog.util.ResultJson;
import com.jf.blog.util.exception.BusinessException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class ApiServiceImpl implements ApiService {
    @Autowired
    private UserinfoService userinfoService;
    @Autowired
    private BlogService blogService;
    @Autowired
    private CommentService commentService;
    @Autowired
    private TagServiceImpl tagService;
    @Autowired
    private BlogMapper blogMapper;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private TimelineMapper timelineMapper;

    @Override
    public Userinfo checkPath(String path) {
        return userinfoService.getOne(new QueryWrapper<Userinfo>()
                .eq("href", path)
        );
    }


    @Override
    public ResultJson getBlogPage(int page) {
        IPage<Blog> iPage = blogService.page(new Page<>(page, 8),
                new QueryWrapper<Blog>()
                        .eq("create_user", LoginUtil.getVipId())
                        .eq("status", 1)
                        .eq("blog_status", 1)
                        .orderByDesc("read_count")
        );
        return ResultJson.success(iPage);
    }

    @Override
    public ResultJson getVipInfo() {
        return ResultJson.success(userinfoService.getById(LoginUtil.getVipId()));
    }

    @Override
    public ResultJson blogDetail(int blogId) {
        Blog blog = checkBlogId(blogId);
        return ResultJson.success(blog);
    }

    private Blog checkBlogId(int blogId) {
        Blog blog = blogService.getById(blogId);
        if (blog == null) {
            throw new BusinessException("编号异常！");
        }
        if (!blog.getCreateUser().equals(LoginUtil.getVipId())
                || blog.getStatus() == 2
                || blog.getBlogStatus() == 2) {
            throw new BusinessException("无权查看");
        }
        return blog;
    }

    @Override
    public ResultJson getCommentPage(int page, int blogId) {
        checkBlogId(blogId);
        IPage<Comment> iPage = commentService.page(new Page<>(page, 5), new QueryWrapper<Comment>()
                .eq("blog_id", blogId)
                .eq("status", 1)
                .orderByDesc("create_time")
        );
        return ResultJson.success(iPage);
    }

    @Override
    public void saveComment(CommentForm form) {
        checkBlogId(form.getBlogId());
        commentService.save(form);
    }

    @Override
    public ResultJson getTagList() {
        List<Tag> list = tagService.list(new QueryWrapper<Tag>()
                .eq("status", 1)
                .eq("create_user", LoginUtil.getVipId())
                .orderByDesc("create_time")
        );
        return ResultJson.success(list);
    }

    @Override
    public ResultJson getBlogByTagId(int page, Integer tagId) {
        IPage<Blog> iPage = null;
        int limit = 8;
        if (tagId == null) {
            iPage = blogService.page(new Page<>(page, limit), new QueryWrapper<Blog>()
                    .eq("status", 1)
                    .eq("blog_status", 1)
                    .eq("create_user", LoginUtil.getVipId())
                    .orderByDesc("apply_time")
            );
        } else {
            iPage = blogMapper.getPage(new Page<>(page, limit), tagId, LoginUtil.getVipId());
        }
        return ResultJson.success(iPage);
    }

    @Override
    public ResultJson getCategoryList() {
        List<Category> list = categoryService.list(new QueryWrapper<Category>()
                .eq("status", 1)
                .eq("create_user", LoginUtil.getVipId())
                .orderByDesc("create_time")
        );
        return ResultJson.success(list);
    }

    @Override
    public ResultJson getBlogByCategoryId(int page, Integer categoryId) {
        IPage<Blog> iPage = blogService.page(new Page<>(page, 8),
                new QueryWrapper<Blog>()
                        .eq("create_user", LoginUtil.getVipId())
                        .eq("status", 1)
                        .eq("blog_status", 1)
                        .eq(categoryId != null, "category_id", categoryId)
                        .orderByDesc("apply_time")
        );
        return ResultJson.success(iPage);
    }

    @Override
    public void updateReadCount(int blogId) {
        blogMapper.updateReadCountInt(blogId);
    }

    @Override
    public ResultJson getBlogPageByYear(int page, String year) {
        IPage<Blog> iPage = timelineMapper.getPageByYear(new Page<>(page, 3), year, LoginUtil.getVipId());
        return ResultJson.success(iPage.getRecords());
    }

    @Override
    public ResultJson getTimeline(Integer createUser) {
        return ResultJson.success(timelineMapper.getTimeline(createUser));
    }
}
